<?php

namespace ua_backend\controllers;

use Yii;
use ua_backend\models\User;
use ua_backend\models\search\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use mdm\admin\components\AccessControl;
use yii\rbac\DbManager;
//use ua_backend\controllers\WxController;
use ua_backend\models\Excel;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                       //'delete' => ['POST'],
                 ],
             ],
            'access' => [
                'class' => AccessControl::className(),
             ],

        ];
    }

    // 待核销
    public function actionIndex()
    {
        $searchModel = new UserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'urlQuery' => Yii::$app->request->queryString
        ]);
    }


    public function actionNotes(){
        $searchModel = new NoteSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,0);

        return $this->render('notes', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'title' => '待处理',
            'urlQuery' => Yii::$app->request->queryString
        ]);
    }

    public function actionNotesDone(){
        $searchModel = new NoteSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams,1);

        return $this->render('notes', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'title' => '已处理',
            'urlQuery' => Yii::$app->request->queryString
        ]);
    }

    // 查看
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    // 发送消息
    public function actionSendMsg(){
        $wx = new Wexin();
        $data = $wx->sendWxMsg($_POST);
        $wx->sendDbMsg(1, $_POST, $_POST['rel_id']);// 0 系统消息，1 客服回复

        Notes::updateAll(['status'=>1],['id'=>$_POST['rel_id']]);
        return $data;
    }

    // 删除
    public function actionDelete($id)
    {
        $user = $this->findModel($id);
        $user->delete();
        return $this->redirect(['index']);
    }

    // 查看模型
    protected function findModel($id)
    {

        if (($model = User::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    function geturl($url){
//        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
//        $output = json_decode($output,true);
//        return $output;
    }

    protected function curlPost($url,$data)
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = true; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = true;
        $params[CURLOPT_SSL_VERIFYHOST] = true;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

}
